<template>
  <div id="homePage">
    <!-- 搜索框 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParam.searchText"
        placeholder="从海量图片中搜索"
        enter-button="搜索"
        size="large"
        @search="onSearch"
      />
    </div>
    <!-- 分类和标签筛选 -->

    <a-tabs v-model:active-key="selectedCategory" @change="onSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
    </a-tabs>
    <div class="tag-bar">
      <span style="margin-right: 8px">标签:</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="onSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" />
    <a-pagination
      style="text-align: right"
      v-model:pageNum="searchParam.pageNum"
      v-model:pageSize="searchParam.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, computed } from 'vue'
import {
  listPictureVoByPageUsingPost,
  listPictureTagCategoryUsingGet,
} from '@/api/pictureController'
import { message } from 'ant-design-vue'
import PictureList from '@/components/PictureList.vue'
const loading = ref(true)
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
//搜索条件
const searchParam = reactive<API.PictureQueryRequest>({
  pageNum: 1,
  pageSize: 15,
  sortField: 'createTime',
  sortOrder: 'descend',
})

const onPageChange = (pageNum: number, pageSize: number) => {
  searchParam.pageNum = pageNum
  searchParam.pageSize = pageSize
  fetchData()
}
async function fetchData() {
  //转换搜索参数
  const params = { ...searchParam, tags: [] as string[] }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })

  loading.value = true
  const res = await listPictureVoByPageUsingPost({ ...params })
  if (res.data.code == 200 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
    loading.value = false
  } else {
    message.error('获取数据失败' + res.data.message)
    loading.value = false
  }
}
//页面加载时获取数据
onMounted(() => {
  getTagAndCategory()
  fetchData()
})
//搜索按钮点击事件
const onSearch = () => {
  // 重置分页
  searchParam.pageNum = 1
  fetchData()
}
//获取分类
const categoryList = ref<string[]>([])
//获取标签
const tagList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const selectedTagList = ref<boolean[]>([])
/**
 * 获取分类和标签
 */
function getTagAndCategory() {
  listPictureTagCategoryUsingGet()
    .then((res) => {
      if (res.data.code === 200) {
        tagList.value = res.data.data.tagList ?? []
        categoryList.value = res.data.data.categoryList ?? []
      } else {
        message.error('获取失败' + res.data.data)
      }
    })
    .catch(() => {
      message.error('获取分类和标签异常')
    })
}
</script>
<style scoped>
#homePage {
  margin-bottom: 16px;
}
#homePage .search-bar {
  max-width: 480px;
  margin: 0 auto 16px;
}
#homePage .tag-bar {
  margin-bottom: 16px;
}
</style>

